A First-Principles-Driven Life

Posts tagged "semantic money":

27 Jan 2023

Reactive Exchanges - SLAMM dunk with Superfluid

Introduction

Superfluid protocol enables money to move between entities continuously in time with no transaction. It is a new paradigm of how the money payment system works. Using this innovative building block, one can build novel applications for payroll, subscriptions, vesting, streaming AMMs, gaming, trade finance, rentals, and NFTs. Click here to read more what is Superfluid.

Though modeling a problem domain explicitly with time is not a novel concept. In fact, functional reactive programming (FRP) that was formulated in a ICFP 97 paper1 demonstrated that we can model animations elegantly and efficiently using continuous time. More than 20 years later, it was Superfluid, the first one that successfully connected the dots between FRP and the domain of money payment system, and has made it available on more than 6 EVM (Ethereum Virtual Machine) chains2.

In this article, we focus on one of its novel applications: different designs of exchanges that can facilitate swaps continuously in time3. By now, you should understand why I shall name them "Reactive Exchanges." Reactive exchanges are types of exchanges modeled with functional reactive programming.

The article expects you to be familiar with the trading terminologies and related innovations on decentralized ledgers. Axo trade whitepaper4, for instance, provides an excellent overview of this domain. Additionally, it would be best if you had an in-depth overview of what is Superfluid is and how it works5.

Different Reactiveness

Throughout the article, we separate the "reactiveness" of an exchange into three primary functions:

  • Contribution: does the input asset enter the exchange continuously in time?
  • Swap: does swap between the input and assets happen continuously in time?
  • Distribution: is output asset distributed continuously in time?

At the end of the article, we will then compare different designs using this classification.

Pseudo Reactive Exchange

The first attempt at having reactive exchange is making only the contribution continuous in time. The actual swaps are done through an external DEX (decentralized exchange), and distribution of the swap output is done through Superfluid IDA6, both happen periodically and are triggered by keepers ("cronjob at scale").

Let's call this "pseudo reactive exchange" since the actual swaps do not continuously in time. Here is a visualization of such exchange:

pseudo-reactive-exchange.png

An example of such exchanges that is in production is Ricochet Exchange, an original and earliest example of how to build an unique exchange using Superfluid. It has been live for over an year, and provides an efficient way for anyone to DCA (dollar cost average)7 invest into on-chain assets.

Reactive Constant Function Market Maker (R-CFMM) Exchange

CFMM (Constant Function Market Maker) was the first class of AMM (Automatic Market Maker) applied to real-world financial markets4. In this article, we look into making one of the earliest and simplest versions of CFMM, Constant Liquidity Product (CLP) used in Uniswap v1 and v2 8 , 9, reactive.

To solve the equations required to make the CLP reactive, I used the python binding of the SageMath framework, a free open-source mathematics software system licensed under the GPL:

from sage.all import var, assume, function, solve

# let's define a handful of symbols
L_a, L_b, T, r, r_a, r_b, t_0, t = var("L_a", "L_b", "T", "r", "r_a", "r_b", "t_0", "t")
assume(t >= t_0)

First, let's define CLP:

from sage.all import var, assume, function, solve

def CLP(x, y, x_prime, y_prime):
    """Constant Liquidity Product Swap"""
    return x * y == x_prime * y_prime
assume(t >= t_0)

Secondly, let's try to use sage to solve the price function for selling asset A (sell amount \(a_\Delta\)) for asset B (get amount \(b_\Delta\)):

def solve_clp_a4b():
    print("# Solve CLP equation for selling A for B instantly\n")
    v_a = var("a_Δ")
    v_b = var("b_Δ")
    clp = CLP(
        L_a,
        L_b,
        L_a + v_a,
        L_b + v_b
    )
    sols = solve(clp, v_b)
    assert(len(sols) == 1)
    print(sols[0])
    print("\n")

Sage spits out the well-known equation as expected:

\[L_{b\Delta} = \frac{L_b * a_\Delta}{L_a + a_\Delta}\]

Now it comes to how to make it reactive; two functions of time need to be solved: one for asset A and another for asset B. There are not enough free variables to solve two equations, but we do know that these two equations should be elated to each other. Here was the stroke of insight, and a magic variable "q" was introduced (and please don't ask more):

def solve_rclp_rtb_bidir():
    print("# Solve Reactive CLP rtb_bidir equation\n")
    cf_a = r_a * (t - t_0)
    cf_b = r_b * (t - t_0)

    q = var("q")
    clp = CLP(
        L_a,
        L_b,
        L_a + cf_a + q * cf_b,
        L_b + cf_b + 1/q * cf_a
    )
    sols = solve(clp, q)
    print("L_{flowswap_a} =", (1/q * cf_a).subs(sols[0]))
    print("L_{flowswap_b} =", (q * cf_b).subs(sols[0]))
    print("\n")

Gratefully, Sage spits out the magic formula we are looking for. Let's call it flowswap formula:

  1. \[L_{flowswap_a} = \frac{-(r_b * t_\Delta - L_b) * r_a * t_\Delta}{r_a * t_\Delta + L_a}\]
  2. \[L_{flowswap_b} = \frac{-(r_a * t_\Delta - L_a) * r_b * t_\Delta}{r_b * t_\Delta + L_b}\]

Here is an illustration of the global view of a reactive CLP exchange:

R-CLP-global-view.png

Another way of seeing it is from how swaps look after "reactification" on the chart of the CLP formula:

R-CLP-formula-view.png

To make the exchange work for more participants, we again need to use IDA from Superfluid money, where the proportion of the flowswap output is determined by the market takers' continuous flow contributions.

Regarding "reactiveness, " market takers always "flowswap" constant flows for another non-linear flow of money, and they are both continuous in time and reactive. However, Superfluid money cannot distribute money through arbitrary formulas; that's why the distribution cannot be reactive unless a bespoke flowswap primitive is a built-in feature of the money.

You can find the work-in-progress prototype of this in the T-REX (Toy Reactive Exchange) Monorepo. If you are interested in making this closer to production, join Superfluid's wave pool program.

Zero-Intermediate-Liquidity Market Maker (ZILMM) Exchange

Another fascinating and unique variation of AMM enabled by Superfluid money is a market maker that requires zero intermediate liquidity, where liquidity goes from LPs to traders directly as constant flows. As a result, a trade-off (or feature) is that such exchanges may not easily facilitate instant swaps.

I shall call such exchange ZILMM: Zero-Intermediate-Liquidity Market Makers. The pioneer of such design is Superfluid's ecosystem project Aqueduct. We will leave the reader to discover more about the project on their own, and we will only include an illustration from them to get a taste of it:

  1. At market liquidity "equilibrium"

    aqueduct-1.png
  2. Traders tilting the "equilibrium"

    aqueduct-2.png

Since all flows involved are constant flows, it should not be a surprise that this design of the reactive exchange ticks all the reactiveness boxes!

Conclusion

Let us review the "reactiveness" of all three Superfluid money enabled market maker designs:

Exchange Type Contribution Swap Distribution Known
/ Reactiveness Reactiveness Reactiveness Reactiveness Projects
Pseudo Yes No No Ricochet
R-CFMM Yes Yes Maybe(a) T-Rex
ZILMM Yes Yes Yes Aqueduct

Note:

a) Distribution with the complex formula required by R-CFMM is viable but not necessarily desirable.

Each design has its pros and cons, and it is beyond the purpose of this article to dive into them.

However, Superfluid money demonstrably enlarged the design surface of AMM. I believe it should be a safe bet that innovations enabled by Superfluid money are not restricted to AMM and other types of financial contracts such as options, futures, interest rate swaps, etc., should have unique Superfluid money enabled designs too.

While reactive exchanges accurately reflects the underlying technology, a more playful term could be SLAMM (Streaming Liquidity Automatic Market Makers). I will end the article with a slogan:

Let's SLAMM dunk it with Superfluid.

Footnotes:

1

Elliott, Conal; Hudak, Paul. "Functional Reactive Animation". Functional Reactive Animation. ICFP ’97. Retrieve 14 July 2018.

2

All deployments of Superfluid protocol can be accessed through Superfluid app.

3

It should not to be confused with the existing financial term continuous trading.

8

Uniswap v1 Protocol Hayden Adams et al. Novembrer 2018. URL: https://docs.uniswap.org/protocol/V1/introduction. Accessed on 15/10/2021.

9

Uniswap v2 Core Hayden Adams et al. March 2020. URL: https://uniswap.org/ whitepaper.pdf. Accessed on 15/10/2021.

Tags: superfluid defi semantic money
24 Oct 2022

First Yellowpaper About Superfluid Protocol, the Beginning of Something Greater

Where Are We

We, builders of the Superfluid Protocol, have come a long way.

EVMv1 of Superfluid Protocol has been live on Polygon (then Matic Network) since a year and a half ago (May 2021). Since then, we have been live in 5 more networks, including Gnosis Chain (then xDAI Chain), Optimism, Arbitrum, Avalanche, and BNB Chain.

The significant features of the protocol are:

We have had many new product announcements, community project launches, and hackathon winners — all of which you can read about on the Superfluid medium blog.

We are fully committed to building products that expand the reach and adoption of the EVMv1 of Superfluid Protocol. For this, we invite everyone to participate in our roadmap public board, helping us shape them together.

Something Greater

First of all, the reason why we are here is that there is a central observation guiding us; consequently, we are motivated by a strong belief in how payment systems can become:

Payment systems in the information age are still modeled after their analog predecessors. We can modernize it.

Meanwhile, we are planning for an extended period of focused building for this cycle. Namely, we want to expand the depth and breadth of the idea behind the Superfluid Protocol.

More Depth

Yellowpapers & Stronger Foundation

As the first step to expand the depth of the protocol, I am happy to announce that the first yellowpaper of its series about the most fundamental part of the Superfluid idea is now available: "Denotational Semantics of General Payment Primitives, and Its Payment System."

The paper first explores the foundation of modern payment systems, which consists of a money distribution model, payment primitives, payment execution systems of financial contracts, and different forms of money mediums. Then the paper uses denotational semantics to formally define payment primitives for modern payment systems. Lastly, this paper also includes an overview of the Superfluid Protocol, a reference implementation of the payment primitives, and its payment system.

Here are some of the topics future yellowpapers will explore:

  • Composing payment primitives of modern payment systems.
  • A deterministic buffer-less modern payment execution environment.
  • A note (EUTXO) based modern payment execution environment.

These are some cross-cutting concerns of modern payment systems; they form a stronger foundation to build future Superfluid Protocol implementations.

Next Generation Architecture

Here is a sneak peek into how that next generation Superfluid Protocol implementation could look like:

superfluid-protocol-ng.png

Execution-Layer Agnostic

The architecture is deliberately execution-layer agnostic to prepare for a many-chains world.

That means deferring the decisions related to the specificity of the execution layer towards as much edge as possible, such as "dev experience overlay," "data availability," etc.

Functional Core & Data Availability

A functional core means that we apply the functional programming style to the core part of the protocol, namely (a) using denotational semantics to define payment primitives, (b) pushing data availability out of the core, (c) and composing payment primitives through combinators.

The advantages of such architecture are that:

  • It ensures we can better reason the system's security,
  • abstracting data availability so that we can try new methods such as gas-optimizing data serializations, ad-hoc merkle tree data rollups, ZK Proofs, FHE (Fully Homomorphic Encryption), etc.,
  • thinking in combinators enables powerful compositionality.

Overlay & Tooling for Dev Experience

From our experience in EVMv1, a distinct separation between core and peripherals (including dev experience overlay) should unshackle creativity in token/note interface design.

In the off-chain world, data indexers and SDKs are the critical pieces to unlock the complete development experience for the dapp developers.

Super Apps Power-Ups

Super Apps will also get power-ups from the more sound core and the better dev experience overlay, so developers should expect to build safer Super Apps easier.

Automation

Unlike EVMv1, where we added the ACL feature for automation later, the off-chain automation system will be part of the integral design of the system from day 1.

More Breadth

Superfluid Protocol EVMv2

We will still maintain our strong focus on EVM blockchains and start to work towards the EVMv2 Superfluid Protocol.

Non-EVM Prototypes

At the same time, bringing ideas behind the Superfluid Protocol everywhere is our longer-term dream. Therefore, to avoid the risk of premature favoritism, we shall not mention any particular blockchains; nonetheless, it suffices to say that a grant-seeking approach will be favored when exploring other chain implementations.

Get Involved

We’re excited to launch our yellowpaper series, and hope it helps evolve the discussion around what a modern payment system could and should look like. We have yet to publicize any research-related job posts, but if our yellowpaper series interests you and you’d like to be part of it, please visit http://jobs.superfluid.fiance or send your CV to jobs@superfluid.finance!

Tags: superfluid semantic money
Other posts
Creative Commons License
A First-Principles-Driven Life by Miao, ZhiCheng is licensed under a Creative Commons Attribution 3.0 License.